package com.cn.springcloudwechat.controller.vaccine.controller;

import com.cn.springcloudwechat.common.ajax.AjaxRes;
import com.cn.springcloudwechat.common.mybatis.Page;
import com.cn.springcloudwechat.common.utils.ErrorUtil;
import com.cn.springcloudwechat.common.utils.ThreadLocalUtil;
import com.cn.springcloudwechat.common.utils.base.Const;
import com.cn.springcloudwechat.common.utils.weixin.util.ConfigReader;
import com.cn.springcloudwechat.controller.base.BaseWeChatController;
import com.cn.springcloudwechat.controller.vaccine.log.LogInfo;
import com.cn.springcloudwechat.controller.vaccine.log.OperateType;
import com.cn.springcloudwechat.mapper.mapper.YmUserPointMapper;
import com.cn.springcloudwechat.model.YmChildrenOrderVaccination;
import com.cn.springcloudwechat.model.YmUser;
import com.cn.springcloudwechat.model.YmUserChild;
import com.cn.springcloudwechat.model.vaccine.YmVaccine;
import com.cn.springcloudwechat.model.vaccine.YmVaccineTypeWithBLOBs;
import com.cn.springcloudwechat.model.weixin.user.Follower;
import com.cn.springcloudwechat.model.weixin.user.WxFollower;
import com.cn.springcloudwechat.redis.RedisUtil;
import com.cn.springcloudwechat.service.personalcenter.service.WxFollowerService;
import com.cn.springcloudwechat.service.personalcenter.service.YmUserPointService;
import com.cn.springcloudwechat.service.personalcenter.service.YmUserService;
import com.cn.springcloudwechat.service.vaccine.vaccineService.VaccineService;
import com.cn.springcloudwechat.service.vaccine.weixinService.WeiXinService;

import net.sf.json.JSONObject;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.cn.springcloudwechat.common.utils.RandomNumberUtil.getRandomNumber;


@Controller
@RequestMapping("/web/vaccine/")
@ResponseBody
public class VaccinePhoneController extends BaseWeChatController{
	
	
	@Autowired
	VaccineService vaccineService;
	
	
	/**
	 * 接种证查询(移动端)
	 * 
	 * @param age(可有可无的查询条件*年龄段)
	 *           
	 * @return List<YmVaccine>
	 */
	@RequestMapping("queryVaccineInoculationPlan")
	public AjaxRes queryVaccineInoculationPlan(AjaxRes ajaxRes,String age,YmUserChild ymUserChild){
	    try {
	    	List<YmVaccine> weekYearPlans = this.vaccineService.queryVaccineInoculationPlanSer(age,ymUserChild);
	        ajaxRes.setSucceed(weekYearPlans,Const.DATA_SUCCEED);
	    }catch (Exception e){
	        ajaxRes.setFailMsg(e.getMessage());
	    }
	    return ajaxRes;
	}
	
	/**
	 * 疫苗关联查询(包括详情)*找到某个疫苗所有所有关联疫苗(客户端)
	 * 
	 * @param YmVaccine YmVaccine
	 *           
	 * @return List<YmVaccine>
	 */
	@RequestMapping("queryVaccineRelevanceDetail")
	public AjaxRes queryVaccineRelevanceDetail(AjaxRes ajaxRes,YmVaccine ymVaccine,@RequestParam("vaccines[]") String[] vaccines){
	    try {
	    	List<YmVaccine> YmVaccines = this.vaccineService.queryVaccineRelevanceDetailSer(vaccines);
	        ajaxRes.setSucceed(YmVaccines,Const.DATA_SUCCEED);
	    }catch (Exception e){
	        ajaxRes.setFailMsg(e.getMessage());
	    }
	    return ajaxRes;
	}
	
	/**
	 * 疫苗周岁的详情查询(*某个疫苗的详情查询(客户端))
	 * 
	 * @param YmVaccine ymVaccine
	 *           
	 * @return List<YmVaccine>
	 */
	@RequestMapping("queryVaccine")
	public AjaxRes queryVaccine(AjaxRes ajaxRes,YmVaccine ymVaccine,Page<Object> page){
	    try {
	    //	List<YmVaccine> YmVaccines = this.vaccineService.queryVaccineSer(ymVaccine);
	    	Page<Object> YmVaccines= this.vaccineService.queryVaccineSer(ymVaccine,page);
	        ajaxRes.setSucceed(YmVaccines,Const.DATA_SUCCEED);
	    }catch (Exception e){
	        ajaxRes.setFailMsg(e.getMessage());
	    }
	    return ajaxRes;
	}
	
	/**
	 * 接种历史(剂次)查询(移动端*接种证点击进入查询)
	 * 
	 * @param vaccineId,parentId
	 *           
	 * @return List<YmVaccine>
	 */
	@RequestMapping("queryVaccineHistoryPlan")
	public AjaxRes queryVaccineHistoryPlan(AjaxRes ajaxRes,Long vaccineTypeId,Long parentId,Long childrenId){
	    try {
	    	List<YmVaccine> weekYearPlans = this.vaccineService.queryVaccineHistoryPlanSer(vaccineTypeId,parentId,childrenId);
	        ajaxRes.setSucceed(weekYearPlans,Const.DATA_SUCCEED);
	    }catch (Exception e){
	        ajaxRes.setFailMsg(e.getMessage());
	    }
	    return ajaxRes;
	}
	/**
	 * 查询历史疫苗(移动端)
	 * 
	 * @param 
	 *           
	 * @return List<YmVaccine>
	 */
	@RequestMapping("queryVaccineHistoryPlanAll")
	public AjaxRes queryVaccineHistoryPlanAll(AjaxRes ajaxRes,Long childrenId){
	    try {
	    	List<YmVaccine> weekYearPlans = this.vaccineService.queryVaccineHistoryPlanAllSer(childrenId);
	        ajaxRes.setSucceed(weekYearPlans,Const.DATA_SUCCEED);
	    }catch (Exception e){
	        ajaxRes.setFailMsg(e.getMessage());
	    }
	    return ajaxRes;
	}
	
	/**
	 * 接种参考查询(移动端)*自费和非自费(isCharge)
	 * 
	 * @param 
	 *           
	 * @return List<YmVaccine>
	 */
	@RequestMapping("queryInoculationConsult")
	public AjaxRes queryInoculationConsult(AjaxRes ajaxRes){
	    try {
	    	List<YmVaccine> weekYearPlans = this.vaccineService.queryInoculationConsultSer();
	        ajaxRes.setSucceed(weekYearPlans,Const.DATA_SUCCEED);
	    }catch (Exception e){
	        ajaxRes.setFailMsg(e.getMessage());
	    }
	    return ajaxRes;
	}
	
	/**
	 * 根据年龄查询参考计划
	 * 
	 * @param String age
	 *           
	 * @return List<YmVaccine>
	 */
	@RequestMapping("queryVaccinePlanAge")
	public AjaxRes queryVaccinePlanAge(AjaxRes ajaxRes,String age){
	    try {
	    	List<YmVaccine> YmVaccines = this.vaccineService.queryVaccinePlanAgeSer(age);
	        ajaxRes.setSucceed(YmVaccines,Const.DATA_SUCCEED);
	    }catch (Exception e){
	        ajaxRes.setFailMsg(e.getMessage());
	    }
	    return ajaxRes;
	}

	/**
	 * 疫苗类型的查询(配置周岁疫苗的时候使用(管理端))
	 *
	 * @param YmVaccineTypeWithBLOBs ymVaccineTypeWithBLOBs
	 *
	 * @return List<YmVaccineTypeWithBLOBs>
	 */
	@RequestMapping("/queryVaccineType")
	public AjaxRes queryVaccineType(AjaxRes ajaxRes, YmVaccineTypeWithBLOBs ymVaccineTypeWithBLOBs, Page<Object> page){
		try {
			// 	List<YmVaccineTypeWithBLOBs> ymVaccineTypes= this.vaccineService.queryVaccineTypeSer(ymVaccineTypeWithBLOBs,page);
			Page<Object> ymVaccineTypes= this.vaccineService.queryVaccineTypeSer(ymVaccineTypeWithBLOBs,page);
			ajaxRes.setSucceed(ymVaccineTypes,Const.DATA_SUCCEED);
		}catch (Exception e){
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 用户手动增加接种记录-根据疫苗ID查询该疫苗的所有接种次数
	 *
	 * @param vaccineTypeId,childId
	 *
	 * @return
	 */
	@RequestMapping("queryAgentByVaccineTypeId")
	public AjaxRes queryAgentByVaccineId(AjaxRes ajaxRes,Long vaccineTypeId, Long childId){
		try {
			List<YmVaccine> ymVaccineList = this.vaccineService.queryAgentByVaccineTypeId(vaccineTypeId,childId);
			ajaxRes.setSucceed(ymVaccineList,Const.DATA_SUCCEED);
		}catch (Exception e){
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}
    /**
     * 用户手动增加接种记录
     *
     * @param ymChildrenOrderVaccination
     *
     * @return
     */
    @RequestMapping("insertYmChildrenOrderVaccinationSer")
    public AjaxRes insertYmChildrenOrderVaccinationSer(AjaxRes ajaxRes,YmChildrenOrderVaccination ymChildrenOrderVaccination){
        try {
           Integer number = this.vaccineService.insterYmChildrenOrderVaccinationByUserInfo(ymChildrenOrderVaccination);
            ajaxRes.setSucceed(number,Const.DATA_SUCCEED);
        }catch (Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }

    /**
     * 用户手动修改接种记录
     *
     * @param ymChildrenOrderVaccination
     *
     * @return
     */
    @RequestMapping("updateYmChildrenOrderVaccinationById")
    public AjaxRes updateYmChildrenOrderVaccinationById(AjaxRes ajaxRes,YmChildrenOrderVaccination ymChildrenOrderVaccination){
        try {
            Integer number = this.vaccineService.updateYmChildrenOrderVaccinationById(ymChildrenOrderVaccination);
            ajaxRes.setSucceed(number,Const.DATA_SUCCEED);
        }catch (Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }
}
